package top.goldenyear.porpoise.framework.web.core.generator.model;

import cn.hutool.v7.core.bean.BeanUtil;
import cn.hutool.v7.core.text.StrUtil;
import com.jfinal.kit.Kv;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import lombok.*;
import lombok.experimental.Accessors;
import top.goldenyear.porpoise.framework.web.core.generator.config.ColumnConfig;
import top.goldenyear.porpoise.framework.web.core.generator.config.GlobalConfig;
import top.goldenyear.porpoise.framework.web.core.generator.config.StrategyConfig;

/**
 * 类属性信息
 *
 * @author airhead
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class FieldMeta {
  /** 属性名称 */
  private String name;

  /** 属性类型 */
  private String type;

  private String simpleType;

  private String comment;

  private ColumnMeta columnMeta;

  private ColumnConfig columnConfig;

  private Kv kvColumnConfig;

  public static FieldMeta covert(
      TableMeta tableMeta, ColumnMeta columnMeta, GlobalConfig globalConfig) {
    FieldMeta fieldMeta = new FieldMeta();
    fieldMeta.setName(columnMeta.getProperty());
    fieldMeta.setType(columnMeta.getPropertyType());
    fieldMeta.setSimpleType(columnMeta.getPropertySimpleType());
    fieldMeta.setComment(columnMeta.getComment());

    StrategyConfig strategyConfig = globalConfig.getStrategyConfig();
    ColumnConfig config = strategyConfig.getColumnConfig(tableMeta.getName(), columnMeta.getName());
    if (config == null) {
      // 全局字段配置，表名为*
      config = strategyConfig.getColumnConfig("*", columnMeta.getName());
    }

    fieldMeta.setColumnConfig(config);
    fieldMeta.setColumnMeta(columnMeta);

    return fieldMeta;
  }

  public void setComment(String comment) {
    // comment 包含回车时可能导致生产代码异常
    this.comment = StrUtil.replace(comment, "\r|\n", " ");
  }

  public String getSupName() {
    return StrUtil.upperFirst(name);
  }

  public String getColumn() {
    return columnMeta.getName();
  }

  public boolean getPrimaryKey() {
    return columnMeta.getPrimaryKey();
  }

  public boolean getAutoIncrement() {
    return columnMeta.getAutoIncrement();
  }

  public Set<String> getImports() {
    Set<String> imports = new HashSet<>();
    // java.util.List<String> >>>>> java.util.List
    // 似乎生成的时候不可能有这样的类型
    if (type.contains("<") && type.endsWith(">")) {
      type = type.substring(0, type.indexOf("<"));
    }

    // 不包含“.”则认为是原始类型，不需要import
    // lang 包不需要显式导入
    if (type.contains(".") && !type.startsWith("java.lang.")) {
      imports.add(type);
    }

    return imports;
  }

  public Boolean getSearch() {
    if (columnConfig == null) {
      return true;
    }

    if ("id".equals(name)) {
      return false;
    }

    return Boolean.TRUE.equals(columnConfig.getSearch());
  }

  public String getSearchDefValue() {
    if (columnConfig == null) {
      return "null";
    }

    return columnConfig.getSearchDefValue();
  }

  public Boolean getForm() {
    if (columnConfig == null) {
      return true;
    }

    return Boolean.TRUE.equals(columnConfig.getForm());
  }

  public Boolean getModel() {
    if (columnConfig == null) {
      return true;
    }

    return Boolean.TRUE.equals(columnConfig.getModel());
  }

  public String getModelDefValue() {
    if (columnConfig == null) {
      return "null";
    }

    return columnConfig.getModelDefValue();
  }

  public Boolean getResult() {
    if (columnConfig == null) {
      return true;
    }

    return Boolean.TRUE.equals(columnConfig.getResult());
  }

  public Integer getResultWidth() {
    if (columnConfig == null) {
      return 100;
    }

    Integer resultWidth = columnConfig.getResultWidth();
    if (resultWidth == null) {
      return 100;
    }

    return resultWidth;
  }

  public String getFormElement() {
    if (columnConfig == null) {
      return "input";
    }

    return columnConfig.getFormElement();
  }

  public Boolean getNumberType() {
    return "Integer".equals(simpleType)
        || "Long".equals(simpleType)
        || "int".equals(simpleType)
        || ("long").equals(simpleType);
  }

  /**
   * 最通用的获取配置的方法
   *
   * @param key
   * @return
   */
  public Object getValue(String key) {
    if (kvColumnConfig == null) {
      if (columnConfig == null) {
        return null;
      }

      Map<String, Object> configMap = BeanUtil.beanToMap(columnConfig);
      kvColumnConfig = Kv.create().set(configMap);
    }

    return kvColumnConfig.get(key);
  }

  public Integer getSearchSortRank() {
    if (columnConfig == null) {
      return 0;
    }

    Integer sortRank = columnConfig.getSearchSortRank();
    if (sortRank == null) {
      return 0;
    }

    return sortRank;
  }

  public Integer getResultSortRank() {
    if (columnConfig == null) {
      return 0;
    }

    Integer sortRank = columnConfig.getResultSortRank();
    if (sortRank == null) {
      return 0;
    }

    return sortRank;
  }

  public Integer getFormSortRank() {
    if (columnConfig == null) {
      return 0;
    }

    Integer sortRank = columnConfig.getFormSortRank();
    if (sortRank == null) {
      return 0;
    }

    return sortRank;
  }
}
